package SFI.form;

import SFI.acceso.Delegado;
import factura.modelo.Cliente;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.event.InputEvent;
import org.zkoss.zul.Messagebox;

/**
 * 
 *
 * @author Romig Sevilla
 * @version 1.0
 * 
 *
 */

public class FormClienteVista extends EntidadForm {

    private int dimension = 4;
    Delegado deleg = Delegado.getInstance();

    public int getDimension() {
        return dimension;
    }

    public void setDimension(int dimension) {
        this.dimension = dimension;
    }

    @Command
    @NotifyChange("dimension")
    public void changeDimension(
            @ContextParam(ContextType.TRIGGER_EVENT) InputEvent change) {
        try {
            int parsed = Integer.parseInt(change.getValue());
            if (parsed > 0) {
                this.dimension = parsed;
            }
        } catch (NumberFormatException nfe) {
        }
    }

    @Command
    @NotifyChange("cliente")
    public void guardar() {
        int result = deleg.insert(this.getCliente());
        if (result == 0) {
            this.setCliente(new Cliente());
            Messagebox.show("Registro Exitoso");
        } else if (result == 1) {
            Messagebox.show("Ya existe el usuario");
            this.setCliente(new Cliente());
        } else if (result == 2 || result == 3) {
            Messagebox.show("Error no documentado");

        }
    }
}
